第 19 期 Python 程式設計入門-作業任務7


  1. 問答申論題
    今天要跟大家介紹 Python dict 物件的一個方法dict.popitem(),這個方法用於移除並返回字典中的一個鍵值對。
    *官方描述:

Remove and return a (key, value) pair from the dictionary. Pairs are returned in LIFO(後進先出,last in, First out) order.
popitem() is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling popitem() raises a KeyError.

*範例


# 一個包含不同資訊的字典
my_dict = {
    'name': 'John',
    'age': 25,
    'city': 'New York',
    'languages': ['Python', 'JavaScript'],
    'is_student': False
}
# 印出移除的內容,隨後顯示移除後的字典內容
print(my_dict.popitem(),my_dict)
  1. 程式設計實作題:
    ```
    #原始ip 位址
    raw_log_file = '128.90.16.86,186.182.189.72,70.25.134.67,153.40.249.245, 58.246.211.25,128.90.16.86,186.182.189.72,170.25.134.67, 10.40.249.245,58.246.211.25'

原始ip位址格式不一致,有空白格存在,故清除空白

log_file_new = raw_log_file.replace(' ','')

將字串拆分,並以','作為拆分標準

set為無序且不允許重複的結構,以set建立則表示重複數值被去除

log_set = set(log_file_new.split(','))

顯示不重複的op位址

for value in log_set:
print( value )
print('不重複ip位址:', len(log_set))
```
https://replit.com/@kuangn22/hw7#main.py







你可能感興趣的文章

Box Model 與 Display Property

Box Model 與 Display Property

2021 區塊鏈開發入門

2021 區塊鏈開發入門

Java 學習筆記 04 – 資料型別轉換

Java 學習筆記 04 – 資料型別轉換






留言討論